//
//  FirstViewController.m
//  GIFShow
//
//  Created by 陈凯 on 15/6/4.
//  Copyright (c) 2015年 CK. All rights reserved.
//

#import "FirstViewController.h"
#import "FirstTableViewController.h"
@interface FirstViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView * backgroundScrollView;
@property (nonatomic, strong) NSMutableArray * tableVCArray;
@property (nonatomic, strong) UIPageControl * pageControl;
@property (nonatomic, strong) UISegmentedControl * pageSegmentControl;
@end

@implementation FirstViewController

-(NSMutableArray*)tableVCArray{
    if (_tableVCArray == nil) {
        _tableVCArray = [[NSMutableArray alloc]init];
    }
    return _tableVCArray;
}
-(UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 90)*0.5, self.view.frame.size.height - 79, 90, 20)];
        _pageControl.numberOfPages = 2;
        _pageControl.currentPage = 0;
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.9];
    }
    return _pageControl;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
    self.backgroundScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height-64)];
    self.backgroundScrollView.delegate = self;
    [self.view addSubview:self.backgroundScrollView];
    self.backgroundScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.backgroundScrollView.frame.size.height-64);
    self.backgroundScrollView.pagingEnabled = YES;
    NSArray * strArray = [[NSArray alloc]initWithObjects:WEBOJOKESURL,WEBOPICSURL, nil];

    [self.pageControl addTarget:self action:@selector(didChangePageControlValue) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.pageControl];
    NSArray * array = @[@"段子",@"逗图"];
    self.pageSegmentControl = [[UISegmentedControl alloc]initWithItems:array];
    self.pageSegmentControl.frame = CGRectMake(50, 27, self.view.frame.size.width - 100, 30);
    self.pageSegmentControl.tintColor = [UIColor colorWithRed:1 green:0.3 blue:0.15 alpha:0.91];
    self.navigationItem.titleView = self.pageSegmentControl;
    [self.pageSegmentControl setSelectedSegmentIndex:0];
    [self.pageSegmentControl addTarget:self action:@selector(didChangeSegmentValue) forControlEvents:UIControlEventValueChanged];
    for (int i = 0; i < 2; i++) {
    FirstTableViewController * firstTVC = [[FirstTableViewController alloc]initWithStyle:UITableViewStylePlain UrlStr:strArray[i]];
    [self addChildViewController:firstTVC];
    [self.tableVCArray addObject:firstTVC];
    firstTVC.view.frame = CGRectMake(self.view.frame.size.width * i,0, self.backgroundScrollView.frame.size.width , self.backgroundScrollView.frame.size.height-30);
    [self.backgroundScrollView addSubview:firstTVC.view];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.backgroundScrollView) {
        
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == self.backgroundScrollView) {
        [self.pageSegmentControl setSelectedSegmentIndex:scrollView.contentOffset.x/scrollView.frame.size.width];
        [self.pageControl setCurrentPage:scrollView.contentOffset.x/scrollView.frame.size.width];
    }
}


-(void)didChangePageControlValue{
    [self.backgroundScrollView setContentOffset:CGPointMake(self.pageControl.currentPage * self.backgroundScrollView.frame.size.width,0) animated:YES];
    [self.pageSegmentControl setSelectedSegmentIndex:self.pageControl.currentPage];
}
-(void)didChangeSegmentValue{
    [self.backgroundScrollView setContentOffset:CGPointMake(self.pageSegmentControl.selectedSegmentIndex * self.backgroundScrollView.frame.size.width,0) animated:YES];
    [self.pageControl setCurrentPage:self.pageSegmentControl.selectedSegmentIndex];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
